package za.org.jcicada.genss.sensor.impl;

import static org.junit.Assert.assertNotNull;

import java.util.Set;

import za.org.jcicada.genss.ServiceURN;
import za.org.jcicada.genss.messages.Result;
import za.org.jcicada.genss.sensor.ISensor;
import za.org.jcicada.genss.sensor.ISensorObserver;
import za.org.jcicada.genss.sensor.ISensorValue;
import za.org.jcicada.genss.sensor.SensorURN;

public class StubSensorObserver implements ISensorObserver {
    
    public SensorURN sensorURN;
    public ServiceURN terminateURN;
    public ISensorValue value;
    public SensorStatusValue statusValue;
    public ISensor sensor;
    public Result result;
    public Set<SensorURN> sensorURNs;
    public int numSensors;
    public int informSensorCount = 0;
    public int informSensorStatusCount = 0;
    public int informSensorTypeCount = 0;
    public boolean disconnected;

    int TIMEOUT = 1000;

    public void clear() {
        sensorURN = null;
    	terminateURN = null;
    	value = null;
    	sensor = null;
    	result = null;
    	sensorURNs = null;
    	numSensors = 0;
    	informSensorCount = 0;
    	informSensorTypeCount = 0;
    	disconnected = false;
    }

    public void informSensor(SensorURN sensorURN, ISensorValue value) {
        this.informSensorCount++;
        this.sensorURN = sensorURN;
        this.value = value;
    }
    
    public void informSensorStatus(SensorURN sensorURN, SensorStatusValue statusValue) {
    	this.informSensorStatusCount++;
    	this.sensorURN = sensorURN;
    	this.statusValue = statusValue;
    }

    public void informSensorType(SensorURN sensorURN, ISensor sensor) {
        this.informSensorTypeCount++;
        this.sensorURN = sensorURN;
        this.sensor = sensor;
    }

    public void informTerminateService(ServiceURN serviceURN) {
        this.terminateURN = serviceURN;
    }

    public void replyAttach(Result result, Set<SensorURN> sensorURNs) {
        this.result = result;
        this.sensorURNs = sensorURNs;
    }

    public void replyDetach(Result result, Set<SensorURN> sensorURNs) {
        this.result = result;
        this.sensorURNs = sensorURNs;
    }
    
    public void replySensorSet(Result result, int numSensors) {
        this.result = result;
        this.numSensors = numSensors;
    }

    public void replySetSamplingStrategy(Result result) {
        this.result = result;
    }

    public void informDisconnect() {
        disconnected = true;
    }

    public Result getResult() {
        int remaining = TIMEOUT;
        while (remaining > 0) {
            if (result != null) {
                break;
            }
            remaining = remaining - 10;
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            } 
        }
        assertNotNull("Expected a result.", result);
        return result;
    }

    public ISensorValue getValue() {
        int remaining = TIMEOUT;
        while (remaining > 0) {
            if (value != null) {
                break;
            }
            remaining = remaining - 10;
            try {
                Thread.sleep(10);
            } catch (InterruptedException ex) {
                throw new RuntimeException(ex);
            } 
        }
        assertNotNull("Expected a value.", value);
        return value;
    }
    
}
